package 多线程.通讯;

public class Storage {
    int[] cells = new int[10];

    int inPos, outPos, count;

    synchronized void put(int num) {
        try {
            while (count==cells.length){
                this.wait();
            }

            cells[inPos] = num;
            System.out.println(inPos+"存入数据"+num);
            inPos+=1;
            if (inPos==cells.length){
                inPos = 0;
            }
            count++;
            this.notify();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    synchronized void get(){
        try {
            while (count==0){
                this.wait();
            }

            int data = cells[outPos];
            System.out.println(outPos+"取出数据"+data);
            outPos+=1;
            if (outPos==cells.length)
                outPos=0;
            count--;

            this.notify();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}
